package com.ruoyi.web.controller.ddsystem;

import java.util.List;

import cn.hutool.core.lang.UUID;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Discount;
import com.ruoyi.system.service.IDiscountService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 优惠卷模板Controller
 * 
 * @author ruoyi
 * @date 2020-04-08
 */
@Controller
@RequestMapping("/system/discount")
public class DiscountController extends BaseController
{
    private String prefix = "system/discount";

    @Autowired
    private IDiscountService discountService;

    @RequiresPermissions("system:discount:view")
    @GetMapping()
    public String discount()
    {
        return prefix + "/discount";
    }

    /**
     * 查询优惠卷模板列表
     */
    @RequiresPermissions("system:discount:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Discount discount)
    {
        startPage();
        List<Discount> list = discountService.selectDiscountList(discount);
        return getDataTable(list);
    }

    /**
     * 导出优惠卷模板列表
     */
    @RequiresPermissions("system:discount:export")
    @Log(title = "优惠卷模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Discount discount)
    {
        List<Discount> list = discountService.selectDiscountList(discount);
        ExcelUtil<Discount> util = new ExcelUtil<Discount>(Discount.class);
        return util.exportExcel(list, "discount");
    }

    /**
     * 新增优惠卷模板
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存优惠卷模板
     */
    @RequiresPermissions("system:discount:add")
    @Log(title = "优惠卷模板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Discount discount)
    {
        discount.setUuid(UUID.randomUUID().toString());
        int a = discountService.insertDiscount(discount);
        return toAjax(a);
    }

    /**
     * 修改优惠卷模板
     */
    @GetMapping("/edit/{uuid}")
    public String edit(@PathVariable("uuid") String uuid, ModelMap mmap)
    {
        Discount discount = discountService.selectDiscountById(uuid);
        mmap.put("discount", discount);
        return prefix + "/edit";
    }

    /**
     * 修改保存优惠卷模板
     */
    @RequiresPermissions("system:discount:edit")
    @Log(title = "优惠卷模板", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Discount discount)
    {
        return toAjax(discountService.updateDiscount(discount));
    }

    /**
     * 删除优惠卷模板
     */
    @RequiresPermissions("system:discount:remove")
    @Log(title = "优惠卷模板", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(discountService.deleteDiscountByIds(ids));
    }
}
